<template>
  <div>
    <div class="i-table-no-border">
      <Table
        highlight-row
        ref="table"
        :columns="columns"
        :data="list"
        :loading="loading"
        size="small"
        :max-height="500"
        @on-filter-change="handleFilterChange"
        @on-sort-change="sortchange"
      >
        <template slot-scope="{ row }" slot="info">
          <a @click="handleDetailPatient(row)">
            <Tooltip
              placement="top"
              content="共享数据"
              v-if="+row.is_share === 1 || +row.is_share_project === 1"
            >
              <Icon type="ios-share" :size="16" />
            </Tooltip>

            {{ row.true_name || row.user_name }}
            {{ row.gender_str }} {{ row.age }}</a
          >
        </template>
        <template slot-scope="{ row }" slot="state">
          <Tag v-if="row.status_name === '无效'" color="red">{{
            row.status_name
          }}</Tag>
          <Tag v-else-if="row.status_name === '跟进中'" color="volcano">{{
            row.status_name
          }}</Tag>
          <Tag v-else-if="row.status_name === '预约'" color="cyan">{{
            row.status_name
          }}</Tag>
          <Tag v-else-if="row.status_name === '到诊'" color="green">{{
            row.status_name
          }}</Tag>
          <Tag v-else-if="row.status_name === '老患者到诊'" color="magenta">{{
            row.status_name
          }}</Tag>
          <Tag v-else-if="row.status_name === '有意向'" color="blue">{{
            row.status_name
          }}</Tag>
          <Tag v-else-if="row.status_name === '未开口'" color="purple">{{
            row.status_name
          }}</Tag>
          <Tag v-else-if="row.status_name" color="orange">{{
            row.status_name
          }}</Tag>
        </template>
        <template slot-scope="{ row }" slot="label">
          <Poptip word-wrap trigger="hover" placement="right" width="400">
            <template v-for="(item, index) in row.tags_name">
              <Tag
                v-show="index < 3"
                :color="item.type == 1 ? 'warning' : 'primary'"
                :key="index"
                >{{ item.tags_name.replace(/[\r\n]/g, "") }}</Tag
              >
            </template>
            <div slot="content">
              <template v-for="(item, index) in row.tags_name">
                <Tag
                  :color="item.type == 1 ? 'warning' : 'primary'"
                  :key="index"
                  >{{ item.tags_name.replace(/[\r\n]/g, "") }}</Tag
                >
              </template>
            </div>
          </Poptip>
        </template>
        <template slot-scope="{ row }" slot="labels">
          <Poptip word-wrap trigger="hover" placement="right" maxWidth="400">
            <Tag
              color="default"
              v-for="(item, index) in row.body_parts"
              :key="index"
              v-if="index < 3"
              >{{ item.replace(/[\r\n]/g, "") }}</Tag
            >
            <div slot="content">
              <Tag
                color="default"
                v-for="(item, index) in row.body_parts"
                :key="index"
                >{{ item.replace(/[\r\n]/g, "") }}</Tag
              >
            </div>
          </Poptip>
          <!-- <Tooltip content="Bottom Center text" placement="bottom">
                        <Tag v-if="+index <= 2" v-for="(item, index) in row.body_parts" :key="index">{{ item }}
                        </Tag>
                        <Tag slot="content" v-for="(item, index) in row.body_parts" :key="index">{{ item }}</Tag>
                    </Tooltip> -->
        </template>
        <template slot-scope="{ row }" slot="disease">
          <span>{{ row.child_diseases_name || row.diseases_name }}</span>
        </template>
        <template slot-scope="{ row }" slot="channel">
          <Badge
            v-if="row.source_name === 0"
            status="success"
            :text="row.source_name"
          />
          <Badge
            v-if="row.source_name === 1"
            status="processing"
            :text="row.source_name"
          />
          <Badge
            v-if="row.source_name === 2"
            status="error"
            :text="row.source_name"
          />
          <Badge
            v-if="row.source_name === 3"
            status="warning"
            :text="row.source_name"
          />
          <span>{{ row.source_name }}</span>
        </template>
        <template slot-scope="{ row, index }" slot="action">
          <a
            v-hasC="{
              name: 'patient-index-edit-info1',
              flag: 'v1/patient/index/edit-info',
            }"
            @click="handleOpenDrawer(row)"
            >编辑</a
          >
          <Divider
            type="vertical"
            v-if="+row.repeat_type > 0"
            v-hasC="{
              name: 'v1-patient-index-repeat-list',
              flag: 'v1/patient/index/repeat-list',
            }"
          />
          <a
            @click="copypatient(row)"
            v-hasC="{
              name: 'v1-patient-index-repeat-list',
              flag: 'v1/patient/index/repeat-list',
            }"
            v-if="+row.repeat_type > 0"
            >重复</a
          >
          <Divider
            type="vertical"
            v-if="+row.is_delete === 1"
            v-hasC="btnShows"
          />
          <a
            @click="delpatient(row)"
            v-if="+row.is_delete === 1"
            v-hasC="btnShows"
            style="color: red;"
            >删除</a
          >
        </template>
        <template slot-scope="{ row, index }" slot="actions">
          <a
            v-hasC="{
              name: 'patient-index-edit-info1',
              flag: 'v1/patient/index/edit-info',
            }"
            @click="handleOpenDrawer(row)"
            >编辑</a
          >
          <Divider type="vertical" v-if="+row.op_type === 2" />
          <a
            v-hasC="{
              name: 'patient-index-cancel-arrive1',
              flag: 'v1/patient/index/cancel-arrive',
            }"
            @click="handleVisit(row)"
            v-if="+row.op_type === 2"
            >撤诊</a
          >
          <Divider
            type="vertical"
            v-if="+row.repeat_type > 0"
            v-hasC="{
              name: 'v1-patient-index-repeat-list',
              flag: 'v1/patient/index/repeat-list',
            }"
          />
          <a
            @click="copypatient(row)"
            v-hasC="{
              name: 'v1-patient-index-repeat-list',
              flag: 'v1/patient/index/repeat-list',
            }"
            v-if="+row.repeat_type > 0"
            >重复</a
          >
        </template>
      </Table>
      <div
        class="ivu-mt iuv-text-center"
        slot="footer"
        style="text-align: center;"
      >
        <Page
          show-total
          show-elevator
          show-sizer
          :total="total"
          :current.sync="params.page"
          :page-size="params.pageSize"
          :page-size-opts="pageOpts"
          @on-change="changepage"
          @on-page-size-change="changepageSize"
        />
      </div>
    </div>
    <patient-detail
      ref="patientDetail"
      :btnShow="btnShow"
      @cancelDrawer="askData"
      @askData="askData"
    />
    <edit-patient ref="editPatient" @patientfun="askData" v-if="!loading" />
    <!-- <return-visit ref="returnVisit" /> -->
    <return-visit ref="returnVisit" @patientfuns="askData" />
    <copymodal ref="copymodal" @askData="askData" :btnShow="btnShows" />
    <FooterToolbar>
      <div
        style="width:100vw;
            height:56px;display:flex;
            justify-content: center;
            align-items: center"
      >
        <Button
          v-hasC="{
            name: 'patient-index-create',
            flag: 'v1/patient/index/create',
          }"
          type="primary"
          class="v-mt-20"
          size="large"
          :loading="addloading"
          @click="handleOpenDrawer(-1)"
          >新增</Button
        >
      </div>
    </FooterToolbar>

    <Modal
      v-model="delModal"
      width="360"
      title="删除确认"
      :draggable="true"
      :closable="false"
      :mask-closable="false"
    >
      <p>删除后患者进入回收站，将不在其他列表展示</p>
      <template #footer>
        <Button type="default" @click="delModal = false">取消</Button>
        <Button type="primary" :loading="btnLoading" @click="handlePatientDel"
          >删除</Button
        >
      </template>
    </Modal>
  </div>
</template>
<script>
import patientDetail from "@/components/patientDetail";
import editPatient from "@/components/editPatient";
import returnVisit from "@/components/returnVisit";
import copymodal from "./copymodal";
import { patientindexcancelarrive, patientindexdelete } from "@/api/hqapi";
export default {
  name: "tablelist",
  props: ["params", "columns", "list", "loading", "total", "currentTab"],
  components: { patientDetail, editPatient, returnVisit, copymodal },
  data() {
    return {
      delModal: false,
      btnLoading: false,
      delId: "",
      btnShows: {
        name: "v1-patient-index-delete4",
        flag: "v1/patient/index/delete",
      },
      pageOpts: [10, 15, 20, 30, 50], // 分页每页数量
      btnShow: [
        {
          name: "patient-index-sub5",
          flag: "v1/patient/index/sub",
        },
        {
          name: "patient-index-edit-remark5",
          flag: "v1/patient/index/edit-remark",
        },
        {
          name: "v1-patient-index-update",
          flag: "v1/patient/index/update",
        },
        {
          name: "patient-callback-callback-common-list4",
          flag: "v1/patient/callback/callback-common-list",
        },
        {
          name: "patient-index-edit-tags10",
          flag: "v1/patient/index/edit-tags",
        },
        {
          name: "patient-index-edit-tags13",
          flag: "v1/patient/index/edit-tags",
        },
        {
          name: "v1-diseasecheck-index-check",
          flag: "v1/diseasecheck/index/check",
        },
      ],
      filteredValue: [],
      filterType: undefined,
      addloading: false,
    };
  },
  methods: {
    initial() {},
    askData() {
      this.$store.commit("admin/account/changeCloseRefresh", false);
      this.$emit("askData", this.params);
    },
    handleVisit(row) {
      this.$Modal.confirm({
        title: "撤诊确认",
        content: "<p>确定给此患者撤诊吗？</p>",
        onOk: () => {
          patientindexcancelarrive({ id: row.id }).then(() => {
            this.$Message.info("撤诊成功");
            this.askData();
          });
        },
      });
    },
    handleDetailPatient(row) {
      if (this.currentTab === "add" && +row.patient_data_type !== 0) {
        this.$refs.patientDetail.handleOpen(row, true);
      } else {
        this.$refs.patientDetail.handleOpen(row);
      }
    },
    copypatient(row) {
      this.$refs.copymodal.initial(row.id);
    },
    handleOpenDrawer(row) {
      if (row === -1) {
        this.addloading = true;
        setTimeout(() => {
          this.addloading = false;
        }, 500);
        this.$refs.editPatient.handleOpenDrawer();
      } else {
        if (this.currentTab === "add" && +row.patient_data_type !== 0) {
          this.$refs.editPatient.handleEdit(row, true);
        } else this.$refs.editPatient.handleEdit(row);
      }
    },
    handlePatientDel() {
      this.btnLoading = true;
      patientindexdelete({ id: this.delId })
        .then(() => {
          this.$Message.info("删除成功");
          this.askData();
          this.btnLoading = false;
          this.delModal = false;
        })
        .catch(() => {
          this.btnLoading = false;
        });
    },
    delpatient(row, index) {
      this.delModal = true;
      this.delId = row.id;
    },
    handleFilterChange(value) {
      this.params.not_callback_start = "";
      this.params.not_callback_end = "";
      this.filteredValue = [];
      if (+value._filterChecked[0] === 1) {
        this.filteredValue = ["1"];
        this.params.not_callback_start = this.$Date()
          .subtract(10, "day")
          .format("YYYY-MM-DD");
        this.params.not_callback_end = this.$Date().format("YYYY-MM-DD");
      } else if (+value._filterChecked[0] === 2) {
        this.filteredValue = ["2"];
        this.params.not_callback_start = this.$Date()
          .subtract(20, "day")
          .format("YYYY-MM-DD");
        this.params.not_callback_end = this.$Date()
          .subtract(11, "day")
          .format("YYYY-MM-DD");
      } else if (+value._filterChecked[0] === 3) {
        this.filteredValue = ["3"];
        this.params.not_callback_start = this.$Date()
          .subtract(30, "day")
          .format("YYYY-MM-DD");
        this.params.not_callback_end = this.$Date()
          .subtract(21, "day")
          .format("YYYY-MM-DD");
      } else if (+value._filterChecked[0] === 4) {
        this.filteredValue = ["4"];
        this.params.not_callback_start = this.$Date()
          .subtract(60, "day")
          .format("YYYY-MM-DD");
        this.params.not_callback_end = this.$Date()
          .subtract(31, "day")
          .format("YYYY-MM-DD");
      } else if (+value._filterChecked[0] === 5) {
        this.filteredValue = ["5"];
        this.params.not_callback_start = "";
        this.params.not_callback_end = this.$Date()
          .subtract(61, "day")
          .format("YYYY-MM-DD");
      } else {
        this.params.not_callback_start = "";
        this.params.not_callback_end = "";
      }
      for (let item of this.columns) {
        if (item.title === "未访天数") {
          item.filteredValue = this.filteredValue;
        }
      }
      this.$emit("askData", this.params);
    },
    patientfun() {
      this.$emit("askData", this.params);
    },
    /* @函数功能: 分页 */
    changepage(e) {
      this.params.page = e;
      this.$emit("askData", this.params);
    },
    /* @函数功能: 每页数量 */
    changepageSize(e) {
      this.params.pageSize = e;
      this.$emit("askData", this.params);
    },
    sortchange(e) {
      if (e.key === "appointment_time_str") {
        if (e.order === "desc") {
          this.params.sort_appointment_time = 2;
        } else if (e.order === "asc") {
          this.params.sort_appointment_time = 1;
        } else {
          this.params.sort_appointment_time = 0;
        }
        this.params.sort_last_callback_time = 0;
        this.params.sort_callback_count = 0;
        this.params.sort_revisiting_time = 0;
        this.params.sort_created_at = 0;
      } else if (e.key === "callback_count") {
        if (e.order === "desc") {
          this.params.sort_callback_count = 2;
        } else if (e.order === "asc") {
          this.params.sort_callback_count = 1;
        } else {
          this.params.sort_callback_count = 0;
        }
        this.params.sort_last_callback_time = 0;
        this.params.sort_appointment_time = 0;
        this.params.sort_revisiting_time = 0;
        this.params.sort_created_at = 0;
      } else if (e.key === "revisiting_time_str") {
        if (e.order === "desc") {
          this.params.sort_revisiting_time = 2;
        } else if (e.order === "asc") {
          this.params.sort_revisiting_time = 1;
        } else {
          this.params.sort_revisiting_time = 0;
        }
        this.params.sort_last_callback_time = 0;
        this.params.sort_appointment_time = 0;
        this.params.sort_callback_count = 0;
        this.params.sort_created_at = 0;
      } else if (e.key === "uncallback_day") {
        for (let item of this.columns) {
          if (item.title === "未访天数") {
            item.filteredValue = [];
          }
        }
        if (e.order === "desc") {
          this.params.sort_last_callback_time = 2;
        } else if (e.order === "asc") {
          this.params.sort_last_callback_time = 1;
        } else {
          this.params.sort_last_callback_time = 0;
        }
        this.params.sort_revisiting_time = 0;
        this.params.sort_appointment_time = 0;
        this.params.sort_callback_count = 0;
        this.params.sort_created_at = 0;
      } else if (e.key === "created_at_str") {
        if (e.order === "desc") {
          this.params.sort_created_at = 2;
        } else if (e.order === "asc") {
          this.params.sort_created_at = 1;
        } else {
          this.params.sort_created_at = 0;
        }
        this.params.sort_revisiting_time = 0;
        this.params.sort_appointment_time = 0;
        this.params.sort_callback_count = 0;
        this.params.sort_last_callback_time = 0;
      } else if (e.key === "belong_assign_time_day") {
        if (e.order === "desc") {
          this.params.sort_belong_assign_time = 2;
        } else if (e.order === "asc") {
          this.params.sort_belong_assign_time = 1;
        } else {
          this.params.sort_belong_assign_time = 0;
        }
        this.params.sort_revisiting_time = 0;
        this.params.sort_appointment_time = 0;
        this.params.sort_callback_count = 0;
        this.params.sort_last_callback_time = 0;
      } else if (e.key === "source_name") {
        if (e.order === "desc") {
          this.params.sort_channel_id = 2;
        } else if (e.order === "asc") {
          this.params.sort_channel_id = 1;
        } else {
          this.params.sort_channel_id = 0;
        }
        this.params.sort_revisiting_time = 0;
        this.params.sort_appointment_time = 0;
        this.params.sort_callback_count = 0;
        this.params.sort_last_callback_time = 0;
      }
      this.params.page = 1;
      this.$emit("askData", this.params);
    },
  },
};
</script>
